import moment from 'moment'

moment.locale('zh_CN')
// var CryptoJS = require('crypto-js');

export function deepCopy(obj, cache = []) {
  // just return if obj is immutable value
  if (obj === null || typeof obj !== 'object') {
    return obj
  }
}
export default {
  reSetArr(arr, len) {
    let allData = []; //用来装处理完的数组
    let currData = []; //子数组用来存分割完的数据
    //循环需要处理的数组
    for (let i = 0; i < arr.length; i++) {
      //将chartArr[i]添加到子数组
      currData.push(arr[i]);
      //在这里求len的余数,如果i不等于0,且可以整除 或者考虑到不满len个或等于len个的情况就要加上  i等于当前数组长度-1的时候
      if ((i != 0 && (i + 1) % len == 0) || i == arr.length - 1) {
        //把currData加到allData里
        allData.push(currData);
        //在这里清空currData
        currData = [];
      }
    };
    return allData;
  },
  getQueryString: function (name) {
    var r = window.location.search.substr(1).match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"));
    if (r) {
      return r[2];
    }
    return null;
  },
  formatTime: function (time, format) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD H:mm:ss')
  },
  formatDate: function (time, format) {
    if (!time) return "";
    if (typeof time == "string") {
      time = parseInt(time, 10);
    }
    return moment(time).format(format || 'YYYY-MM-DD')
  },
  firstDayOfMonth: function () {
    return moment().startOf('month');
  },
  endDayOfMonth: function () {
    return moment().endOf('month')
  },

  currentYear: function () {
    return moment().year()
  },

  currentMonth: function () {
    return moment().month() + 1
  },

  formatNumber: function (val, fixed) {
    return parseFloat(val).toFixed(fixed)
  },

  toNumber: function (val) {
    return Number(val)
  },
  isIOS: function () {
    var u = navigator.userAgent
    return u.indexOf('iPhone') > -1
  },
  isIpad: function () {
    var u = navigator.userAgent
    var isTablet = /(?:iPad|PlayBook)/.test(u) || (/(?:Android)/.test(u) && !/(?:Mobile)/.test(u)) || (/(?:Firefox)/.test(u) && /(?:Tablet)/.test(u))
    return isTablet
  },
//判断是否是微信浏览器的函数
 isWeiXin:function(){
  //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
  var ua = window.navigator.userAgent.toLowerCase();
  //通过正则表达式匹配ua中是否含有MicroMessenger字符串
  if(ua.match(/MicroMessenger/i) == 'micromessenger'){
  return true;
  }else{
  return false;
  }
},
  isEmptyObject: function (e) {
    var t;
    for (t in e)
      return !1;
    return !0
  },
  debounce: function (func, delay) {
    clearTimeout(window.debounceTimer)
    window.debounceTimer = setTimeout(function () {
      func()
    }, delay)
  },

  install: function (Vue) {
    Vue.filter('formatDate', this.formatDate)
    Vue.filter('formatTime', this.formatTime)
    Vue.filter('formatNumber', this.formatNumber)
    Vue.filter('toNumber', this.toNumber)

    var isEmptyObject = this.isEmptyObject
    Vue.mixin({
      methods: {
        isEmptyObject: isEmptyObject,
      }
    })
  }


}
